<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" :width="700">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="70px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item class="" label="订单编号" prop="orderCode">
            <el-input v-model="formData.orderCode" disabled placeholder="请输入"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品名称" prop="name">
            <el-input v-model="formData.name" clearable placeholder="请输入"/>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="长度" prop="len">
            <el-input v-model="formData.len" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="JR/INT/SR" prop="type">
            <el-input v-model="formData.type" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="交期" prop="expiryDate">
            <el-input v-model="formData.expiryDate" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="型号" prop="flex">
            <el-input v-model="formData.flex" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="数量" prop="num">
            <el-input v-model="formData.num" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图档名" prop="drawing">
            <el-input v-model="formData.drawing" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="面漆" prop="topcoat">
            <el-input v-model="formData.topcoat" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="eanCode" prop="eanCode">
            <el-input v-model="formData.eanCode" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="partId" prop="partId">
            <el-input v-model="formData.partId" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="submitForm" ref="subBtn">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
  import * as orderApi from '@/api/hcxly/order/Index'
  import { FormRules } from 'element-plus'

  defineOptions({ name: 'orderEditForm' });

  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
    id: '',
    partId: '',
    orderCode: '',
    name: '',
    len: '',
    type: '',
    expiryDate: '',
    flex: '',
    num: '',
    drawing: '',
    topcoat: '',
    eanCode: '',
    oldnum: ''
  })

  //打开弹窗
  const open = async (row) => {
    console.log(row)
    dialogVisible.value = true
    dialogTitle.value = t('修改')
    formData.value.id = row.id
    formData.value.orderCode = row.orderCode
    formData.value.name = row.name
    formData.value.len = row.len
    formData.value.type = row.type
    formData.value.expiryDate = row.expiryDateStr
    formData.value.flex = row.flex
    formData.value.num = row.num
    formData.value.drawing = row.drawing
    formData.value.topcoat = row.topcoat
    formData.value.eanCode = row.eanCode
    formData.value.partId = row.partId
    formData.value.oldnum = row.num;
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  //表单验证
  const formRules = reactive<FormRules>({
    // name: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
    // ip: [{ required: true, message: '轻重不能为空', trigger: 'blur' }],
    // port: [{ required: true, message: '裁剪尺寸不能为空', trigger: 'blur' }],
  })

  const formRef = ref() // 表单 Ref
  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    console.log(formData.value)
    // 校验表单
    // if (!formRef) return
    // const valid = await formRef.value.validate()
    // if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value
      console.log(data)
      await orderApi.editOrderDetail(data)
      message.success(t('修改成功'))
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')

      // resetForm()
    } finally {
      formLoading.value = false
    }
  }

</script>
